package com.mrd.shiptask_shipowner.activity;

import java.util.HashMap;
import java.util.Map;

import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.constants.HttpConstant;
import com.mrd.shiptask_shipowner.serviecs.BizNetworkService;
import com.mrd.shiptask_shipowner.serviecs.initdatadao.InitDatasDaoImpl;
import com.mrd.shiptask_shipowner.shiareutil.SharePreferenceUtil;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.HttpUrlConstant;
import com.mrd.shiptask_shipowner.unit_tools.SysApplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class NewsDetailActivity extends Activity {
	private NewsDetailActivity THISAC = NewsDetailActivity.this;
	private Button goBackBt, okBt;
	private TextView nameTv, timeTv, contentTv;
	private SharePreferenceUtil sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_detail_main);
		sp = new SharePreferenceUtil(THISAC, HttpConstant.SAVE_USER);
		setInitView();
		hasReceiver();
		SysApplication.getInstance().addActivity(this);

	}

	private void hasReceiver() {
		// TODO Auto-generated method stub
		// 存在消息推送内容，则进行处理
		if (getIntent().hasExtra("getPushValueOfKey")) {
			try {
				FunctionOfActivity.progressDialogShow(THISAC);
				String getMessageKey = getIntent().getExtras().getString(
						"getPushValueOfKey");
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("messageId", getMessageKey);
				new Thread(new BizNetworkService(THISAC, handler,
						HttpUrlConstant.GET_MESSAGE_BY_KEY, params)).start();
			} catch (Exception e) {
			}
		}
	}

	private void setInitDatas(String fromName, String fromTime, String content) {
		// TODO Auto-generated method stub
		if ("1".equals(fromName)) {
			fromName = "系统消息";
		} else if ("2".equals(fromName)) {
			fromName = "义务消息";
		} else if ("3".equals(fromName)) {
			fromName = "其他消息";
		}
		nameTv.setText(fromName);
		timeTv.setText(fromTime);
		contentTv.setText(content);
	}

	private void setInitView() {
		// TODO Auto-generated method stub
		goBackBt = (Button) findViewById(R.id.go_back);
		okBt = (Button) findViewById(R.id.ok_bt);
		goBackBt.setOnClickListener(ocl);
		okBt.setOnClickListener(ocl);
		nameTv = (TextView) findViewById(R.id.nametv);
		timeTv = (TextView) findViewById(R.id.timetv);
		contentTv = (TextView) findViewById(R.id.contenttv);
		/**
		 * 跳转数据接续
		 */
		String mid = getIntentValue("mid");
		String title = getIntentValue("title");
		String time = getIntentValue("time");
		String group = getIntentValue("group");
		String content = getIntentValue("content");
		String action = getIntentValue("action");
		String url = getIntentValue("url");
		setInitDatas(group, time, content);
		/**
		 * 已读时确认按钮隐藏
		 */
		if (getIntent().hasExtra("type")
				&& getIntent().getBooleanExtra("type", true)) {
			okBt.setVisibility(View.GONE);
		}
	}

	private String getIntentValue(String key) {
		return getIntent().hasExtra(key) == false ? "" : getIntent()
				.getStringExtra(key);
	}

	private final OnClickListener ocl = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			try {
				switch (arg0.getId()) {
				case R.id.go_back:
					finish();
					break;
				case R.id.ok_bt:
					Map<String, Object> pramas = new HashMap<String, Object>();
					pramas.put("ship", sp.getShip_id());
					pramas.put("mids", getIntentValue("mid"));
					new Thread(new BizNetworkService(THISAC, handler,
							HttpUrlConstant.SUBMIT_MESSAGE_READED, pramas))
							.start();
					finish();
					break;
				default:
					break;
				}
			} catch (Exception e) {

			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Bundle data = msg.getData();
			if (!data.isEmpty()) {
				switch (msg.what) {
				case HttpUrlConstant.GET_MESSAGE_BY_KEY:
					FunctionOfActivity.makeToaskShow(THISAC,
							data.getString("msg"));
					if (data.getBoolean("ispass")) {
						String mid = data.getString("mid");
						String title = data.getString("title");
						String time = data.getString("time");
						String group = data.getString("group");
						String content = data.getString("content");
						String action = data.getString("action");
						String url = data.getString("url");
						setInitDatas(group, time, content);
					}

					break;

				default:
					break;
				}
			}
		};
	};
}
